package uicgas.register.GUI;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

import uicgas.globaldata.ControlMessage;
import uicgas.globaldata.Messages;
import uicgas.globaldata.Pump;
import uicgas.globaldata.SelfAnnouncement;
import uicgas.globaldata.States;
import uicgas.register.RegisterLogic;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

/**
 * the class that defines all of the actions and logic for the main register screen.
 * @author Steve
 */
public class MainRegister extends JFrame
{
	private JLayeredPane contentPane;
	private PumpSelectionScreen thePumpScreen;
	private Button Assistance;
	private Button[] pump = new Button[21];
	private RegisterLogic register;

	/**
	 * Launch the application, a default launcher for the register screen.
	 * @author Steve
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					//MainRegister frame = new MainRegister();
					//frame.setVisible(true);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame, and all of its components that will be in use
	 * @param RegisterLogic - need register logic for all available pumps and extra logic and communications.
	 * @author Steve
	 */
	public MainRegister(final RegisterLogic register)
	{
		this.register = register;
		setResizable(false);
		this.setTitle("UIC Gas - Register System");
		setAlwaysOnTop(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 500);
		Dimension dim = new Dimension();
		dim.height = 500;
		dim.width = 500;
		this.setPreferredSize(new Dimension(500, 500));
		contentPane = new JLayeredPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		Panel MainRegister = new Panel();
		MainRegister.setBounds(5, 15, 474, 442);
		MainRegister.setFocusable(false);
		contentPane.add(MainRegister);
		MainRegister.setLayout(null);

		JPanel Pumps = new JPanel();
		Pumps.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent arg0) {
				callForUpdate();				
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
				callForUpdate();
			}
		});
		Pumps.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.LOWERED,
				null, null, null, null), new EtchedBorder(EtchedBorder.LOWERED,
				null, null)));
		Pumps.setBounds(0, 0, 335, 452);
		MainRegister.add(Pumps);

		pump[1] = new Button("Pump 1");
		pump[1].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(1, register);

			}
		});
		pump[1].setBounds(10, 10, 75, 75);
		pump[1].setBackground(Color.RED);

		pump[2] = new Button("Pump 2");
		pump[2].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				JOptionPane.showMessageDialog(null, "clicked Pump2", "message",
						0);
				initiatePumpScreen(2, register);
			}
		});
		pump[2].setBounds(91, 10, 75, 75);
		pump[2].setBackground(Color.RED);

		pump[3] = new Button("Pump 3");
		pump[3].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(3, register);

			}
		});
		pump[3].setBounds(172, 10, 75, 75);
		pump[3].setBackground(Color.RED);

		pump[4] = new Button("Pump 4");
		pump[4].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(4, register);

			}
		});
		pump[4].setBounds(253, 10, 75, 75);
		pump[4].setBackground(Color.RED);

		pump[5] = new Button("Pump 5");
		pump[5].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(5, register);

			}
		});
		pump[5].setBounds(10, 98, 75, 75);
		pump[5].setBackground(Color.RED);

		pump[6] = new Button("Pump 6");
		pump[6].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(6, register);

			}
		});
		pump[6].setBounds(91, 98, 75, 75);
		pump[6].setBackground(Color.RED);

		pump[7] = new Button("Pump 7");
		pump[7].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(7, register);

			}
		});
		pump[7].setBounds(172, 98, 75, 75);
		pump[7].setBackground(Color.RED);

		pump[8] = new Button("Pump 8");
		pump[8].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(8, register);

			}
		});
		pump[8].setBounds(253, 98, 75, 75);
		pump[8].setBackground(Color.RED);

		pump[9] = new Button("Pump 9");
		pump[9].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(9, register);

			}
		});
		pump[9].setBounds(10, 185, 75, 75);
		pump[9].setBackground(Color.RED);
		pump[9].setForeground(Color.BLACK);

		pump[10] = new Button("Pump 10");
		pump[10].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//JOptionPane.showMessageDialog(null, "Clicked Pump 10",
				//		"Message", 0);
				initiatePumpScreen(10, register);

			}
		});
		pump[10].setBounds(91, 185, 75, 75);
		pump[10].setBackground(Color.RED);
		pump[10].setForeground(Color.BLACK);

		pump[11] = new Button("Pump 11");
		pump[11].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(11, register);

			}
		});
		pump[11].setBounds(172, 185, 75, 75);
		pump[11].setBackground(Color.RED);
		pump[11].setForeground(Color.BLACK);

		pump[12] = new Button("Pump 12");
		pump[12].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(12, register);

			}
		});
		pump[12].setBounds(253, 185, 75, 75);
		pump[12].setBackground(Color.RED);
		pump[12].setForeground(Color.BLACK);

		pump[13] = new Button("Pump 13");
		pump[13].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(13, register);

			}
		});
		pump[13].setBounds(10, 274, 75, 75);
		pump[13].setBackground(Color.RED);
		pump[13].setForeground(Color.BLACK);

		pump[14] = new Button("Pump 14");
		pump[14].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(14, register);

			}
		});
		pump[14].setBounds(91, 274, 75, 75);
		pump[14].setBackground(Color.RED);
		pump[14].setForeground(Color.BLACK);

		pump[15] = new Button("Pump 15");
		pump[15].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(15, register);

			}
		});
		pump[15].setBounds(172, 274, 75, 75);
		pump[15].setBackground(Color.RED);
		pump[15].setForeground(Color.BLACK);

		pump[16] = new Button("Pump 16");
		pump[16].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(16, register);

			}
		});
		pump[16].setBounds(253, 274, 75, 75);
		pump[16].setBackground(Color.RED);
		pump[16].setForeground(Color.BLACK);

		pump[17] = new Button("Pump 17");
		pump[17].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(17, register);

			}
		});
		pump[17].setBounds(10, 367, 75, 75);
		pump[17].setBackground(Color.RED);
		pump[17].setForeground(Color.BLACK);

		pump[18] = new Button("Pump 18");
		pump[18].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(18, register);

			}
		});
		pump[18].setBounds(91, 367, 75, 75);
		pump[18].setBackground(Color.RED);
		pump[18].setForeground(Color.BLACK);

		pump[19] = new Button("Pump 19");
		pump[19].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(19, register);

			}
		});
		pump[19].setBounds(172, 367, 75, 75);
		pump[19].setBackground(Color.RED);
		pump[19].setForeground(Color.BLACK);

		pump[20] = new Button("Pump 20");
		pump[20].addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				//				JOptionPane.showMessageDialog(null, "Clicked Pump 1",
				//						"Message", 0);
				initiatePumpScreen(20, register);

			}
		});
		pump[20].setBounds(253, 367, 75, 75);
		pump[20].setBackground(Color.RED);
		pump[20].setForeground(Color.BLACK);
		Pumps.setLayout(null);
		for (int i = 1; i < pump.length; i++)
		{
			Pumps.add(pump[i]);
		}

		Button CustAssistance = new Button("Customer Assistance");
		Assistance = CustAssistance;
		CustAssistance.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				callForUpdate();
				InitiateCustomerAssistanceQueue(register);
			}
			@Override
			public void mouseEntered(MouseEvent arg0) {
				callForUpdate();
			}
			@Override
			public void mouseExited(MouseEvent arg0) {
				callForUpdate();
			}
		});
		CustAssistance.setBackground(new Color(255, 140, 0));
		CustAssistance.setBounds(341, 10, 123, 123);
		MainRegister.add(CustAssistance);

		JPanel PumpStatusLegend = new JPanel();
		PumpStatusLegend.setBorder(new BevelBorder(BevelBorder.LOWERED, null,
				null, null, null));
		PumpStatusLegend.setBounds(341, 139, 123, 303);
		MainRegister.add(PumpStatusLegend);
		PumpStatusLegend.setLayout(null);

		Label pumpStatLabel = new Label("  Pump Status Legend");
		pumpStatLabel.setAlignment(Label.CENTER);
		pumpStatLabel.setBounds(2, 7, 119, 22);
		PumpStatusLegend.add(pumpStatLabel);

		Button readyButton = new Button("Ready");
		readyButton.setBounds(39, 48, 45, 22);
		readyButton.setForeground(Color.BLACK);
		readyButton.setEnabled(false);
		readyButton.setBackground(Color.GREEN);
		readyButton.setFocusable(false);
		PumpStatusLegend.add(readyButton);

		Label pumpreadyLabel = new Label("Pump is Ready");
		pumpreadyLabel.setAlignment(Label.CENTER);
		pumpreadyLabel.setBounds(2, 70, 119, 22);
		PumpStatusLegend.add(pumpreadyLabel);

		Button inUseButton = new Button("In Use");
		inUseButton.setForeground(Color.BLACK);
		inUseButton.setFocusable(false);
		inUseButton.setEnabled(false);
		inUseButton.setBackground(Color.YELLOW);
		inUseButton.setBounds(39, 122, 45, 22);
		PumpStatusLegend.add(inUseButton);

		Label pumpInUseLabel = new Label("Pump is In Use");
		pumpInUseLabel.setAlignment(Label.CENTER);
		pumpInUseLabel.setBounds(2, 145, 119, 22);
		PumpStatusLegend.add(pumpInUseLabel);

		Button outOfOrderButton = new Button("Out of Order");
		outOfOrderButton.setForeground(Color.BLACK);
		outOfOrderButton.setFocusable(false);
		outOfOrderButton.setEnabled(false);
		outOfOrderButton.setBackground(Color.RED);
		outOfOrderButton.setBounds(24, 223, 75, 22);
		PumpStatusLegend.add(outOfOrderButton);

		Label outofOrderLabel = new Label("Pump is Out of Order");
		outofOrderLabel.setAlignment(Label.CENTER);
		outofOrderLabel.setBounds(2, 246, 119, 22);
		PumpStatusLegend.add(outofOrderLabel);

		JPanel panel = new JPanel();
		panel.setBounds(5, 5, 474, 452);
		contentPane.add(panel);

		//finalize layout
		this.pack();
		callForUpdate();
		updateAllButtons();

	}

	/**
	 * this method starts the CustomerAssistanceQueue
	 * @param Register
	 */
	protected void InitiateCustomerAssistanceQueue(RegisterLogic Register)
	{
		SelfAnnouncement me = new SelfAnnouncement(Register.getID(),
				Register.getID());
		ControlMessage getpumps = new ControlMessage(
				Messages.ANNOUNCEALLCONNECTEDPUMPS, me);
		Register.getCommunication().sendData(getpumps);
		Pause(1);
		RegisterQueue assistance = new RegisterQueue(Register);
		assistance.updateList(register.getAssistanceQueue().getQueue());
		assistance.setVisible(true);
	}

	/**
	 * this method initializes the desired pump action screen.
	 * @author Steve
	 * @param buttonNumber
	 * @param register
	 */
	private void initiatePumpScreen(int buttonNumber, RegisterLogic register)
	{
		Pump thePump = register.allPumps[buttonNumber-1];
		if (thePump == null)
		{
			JOptionPane.showMessageDialog(null,
					"Error: This Pump is not avalible at this time.",
					"ERROR Message", JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			if (thePump.getID() == -1)// ^ thePump.getID() > 20)
			{
				JOptionPane.showMessageDialog(null,
						"Error: This Pump is not avalible at this time.",
						"ERROR Message", JOptionPane.ERROR_MESSAGE);
			}
			else
			{
				this.thePumpScreen = new PumpSelectionScreen(thePump, register);
				thePumpScreen.setAlwaysOnTop(true);
				thePumpScreen.setVisible(true);
			}
		}
	}

	/**
	 * this method will cause the assistance button to blink several times.
	 * @author Steve
	 */
	public void blinkAssistance()
	{
		Pause(100);
		Assistance.setBackground(Color.DARK_GRAY);
		Toolkit.getDefaultToolkit().beep();
		Pause(100);
		Assistance.setBackground(Color.ORANGE);
		Pause(100);
		Assistance.setBackground(Color.DARK_GRAY);
		Toolkit.getDefaultToolkit().beep();
		Pause(100);
		Assistance.setBackground(Color.ORANGE);
		Pause(100);
		Assistance.setBackground(Color.DARK_GRAY);
		Toolkit.getDefaultToolkit().beep();
		Pause(100);
		Assistance.setBackground(Color.ORANGE);
		Pause(100);
		Assistance.setBackground(Color.DARK_GRAY);
		Toolkit.getDefaultToolkit().beep();
		Pause(100);
		Assistance.setBackground(Color.ORANGE);
		Pause(100);
		Assistance.setBackground(Color.DARK_GRAY);
		Toolkit.getDefaultToolkit().beep();
		Pause(100);
		Assistance.setBackground(Color.ORANGE);
		Pause(100);
		Assistance.setBackground(Color.DARK_GRAY);
		Toolkit.getDefaultToolkit().beep();
		Pause(100);
		Assistance.setBackground(Color.ORANGE);

	}

	/**
	 * this method causes the system to pause momentarily
	 * 
	 * @author Steve
	 * @param iTimeInMiliSeconds
	 */
	private void Pause(int iTimeInMiliSeconds)
	{
		long time_start, time_end;

		time_start = System.currentTimeMillis();
		time_end = System.currentTimeMillis() + (iTimeInMiliSeconds);

		do
		{
			time_start = System.currentTimeMillis();

		}
		while (time_start < time_end);
		{
			//do nothing, just wait
		}
	}
	private void callForUpdate()
	{
		SelfAnnouncement me = new SelfAnnouncement(register.getID(),
				register.getID());
		ControlMessage getmyPumps = new ControlMessage(
				Messages.ANNOUNCEALLCONNECTEDPUMPS, me);
		register.getCommunication().sendData(getmyPumps);
	}

	/**
	 * this method is provided to update all of the pump buttons to their correct State
	 * 
	 * @author Steve
	 */
	public void updateAllButtons()
	{
		int id=0;
		for (int i = 1; i < pump.length; i++)
		{
			States theState = null;
			//System.out.println("I is - " + i);
			try
			{
				theState = register.allPumps[i-1].getState();
			}
			catch (NullPointerException e)
			{
				// TODO Auto-generated catch block
				//e.printStackTrace();
			}
			//id = register.allPumps[i-1].getID();
			//if(id==-1)
			//{
			//	id=1;
			//}
			//if(id==0)
			//{
			//	id=1;
			//}
			if (theState == null)
			{
				pump[i].setEnabled(false);
				pump[i].setBackground(Color.DARK_GRAY);
			}
			else
			{
				pump[i].setEnabled(true); //turn the button back on if it was off
				//check for the different pumpStates
				if (theState == (States.ACTIVATED))
				{
					if (register.allPumps[i-1].getID() != -1)
					{
						//change the corresponding button to yellow
						pump[i].setBackground(Color.YELLOW);
					}
					else
					{
						pump[i].setBackground(Color.RED);
					}
				}
				if (theState == (States.DEACTIVATED))
				{
					//System.out.println("THE PUMP "
					//		+ register.allPumps[i-1].getID() + " IS READY");
					//we don't want a generic initilized pump to be avalible on the pump screen.
					if (register.allPumps[i-1].getID() != -1)
					{
						//change the corresponding button to Green
						pump[i].setBackground(Color.GREEN);
					}
					else
					{
						pump[i].setBackground(Color.DARK_GRAY);
						pump[i].setEnabled(false);
					}
				}
				if (theState == (States.DECOMMISSIONED))
				{
					//change the corresponding button to red
					pump[i].setBackground(Color.RED);
				}
			}
		}
	}
}
